﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities;
using Entities.IRepository;
using BLL;
using DAL;

namespace HeThongNhaSach
{
	public partial class frmTimKiem : Form
	{
		public frmMain frmMain;
		public frmTimKiem()
		{
			InitializeComponent();
		}

		private void btnHuy_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void frmTimKiem_Load(object sender, EventArgs e)
		{
			txtTimTheoTen.Enabled = false;
			grbTimTheoGia.Enabled = false;
			grbTimTheoLoai.Enabled = false;
			txtTimTheoMa.Enabled = false;
			btnTimKiem.Enabled = true;
			btnTimKiem.Enabled = false;
		}

		private void btnTimKiem_Click(object sender, EventArgs e)
		{
			SachBaoRepository _SBRepo = new SachBaoRepository();
			List<SachBao> lsb = _SBRepo.LayTatCa().ToList();
			if (chkTimTheoTen.Checked && !string.IsNullOrEmpty(txtTimTheoTen.Text.Trim()))
			{
				lsb = lsb.FindAll(delegate(SachBao s)
				{
					return s.TenSachBao.Contains(txtTimTheoTen.Text.Trim());
				});
			}
			if (chkTimTheoMa.Checked && !string.IsNullOrEmpty(txtTimTheoMa.Text.Trim()))
			{
				lsb = lsb.FindAll(delegate(SachBao s)
				{
					return s.MaSachBao.Contains(txtTimTheoMa.Text.Trim());
				});
			}
			if (chkTimTheoGia.Checked)
			{
				try
				{
					float min = float.Parse(txtMin.Text);
					float max = float.Parse(txtMax.Text);
					lsb = lsb.FindAll(delegate(SachBao s)
					{
						return s.GiaBan >= min && s.GiaBan <= max;
					});
				}
				catch
				{
					MessageBox.Show("Nhập khoảng giá bán cần tìm");
					return;
				}
			}
			if (chkTimTheoLoai.Checked)
			{
				lsb = lsb.FindAll(delegate(SachBao sb)
				{
					if (rdTimTheoSach.Checked)
						return sb is Sach;
					else
					{
						if (rdTimTheoBao.Checked)
							return sb is Bao;
						else
							return sb is TapChi;
					}
				});
			}
			if (lsb.Count <= 0)
			{
				MessageBox.Show("Không tìm thấy sách báo phù hợp");
			}
			else
			{
				frmMain.tabControl1.SelectedTab = frmMain.tabSachBao;
				frmMain.datasachbao.DataSource = new List<SachBao>();
				frmMain.datasachbao.DataSource = lsb;

				this.Close();
			}
		}

		private void chkTimTheoGia_CheckedChanged(object sender, EventArgs e)
		{
			if (chkTimTheoGia.Checked)
			{
				grbTimTheoGia.Enabled = true;
				btnTimKiem.Enabled = true;
			}
			else
			{
				grbTimTheoGia.Enabled = false;
				if (chkTimTheoMa.Checked || chkTimTheoTen.Checked || chkTimTheoLoai.Checked) ;
				else
					btnTimKiem.Enabled = false;
			}
		}

		private void chkTimTheoTen_CheckedChanged(object sender, EventArgs e)
		{
			if (chkTimTheoTen.Checked)
			{
				txtTimTheoTen.Enabled = true;
				btnTimKiem.Enabled = true;
			}
			else
			{
				txtTimTheoTen.Enabled = false;
				if (chkTimTheoMa.Checked || chkTimTheoTen.Checked || chkTimTheoGia.Checked) ;
				else
					btnTimKiem.Enabled = false;
			}
		}

		private void chkTimTheoMa_CheckedChanged(object sender, EventArgs e)
		{
			if (chkTimTheoMa.Checked)
			{
				txtTimTheoMa.Enabled = true;
				btnTimKiem.Enabled = true;
			}
			else
			{
				txtTimTheoMa.Enabled = false;
				btnTimKiem.Enabled = false;
			}
		}

		private void chkTimTheoLoai_CheckedChanged(object sender, EventArgs e)
		{
			if (chkTimTheoLoai.Checked)
			{
				grbTimTheoLoai.Enabled = true;
				btnTimKiem.Enabled = true;
			}
			else
			{
				grbTimTheoLoai.Enabled = false;
				if (chkTimTheoMa.Checked || chkTimTheoTen.Checked || chkTimTheoGia.Checked) ;
				else
					btnTimKiem.Enabled = false;
			}
		}
	}
}
